<template>
  <div class="container">
      <van-form>
        <div class="thbz" v-if="form.auditReason">{{ form.auditReason }}</div>
        <div class="qiye-info">
            <div class="title">人才信息</div>
            <van-cell-group inset>
                <!-- <van-field
                v-model="form.talentCode"
                name="talentCode"
                label="人才编码："
                placeholder=""
                readonly
                /> -->
                <van-field
                v-model="form.name"
                name="name"
                label="姓名："
                placeholder=""
                readonly
                />
                <van-field
                v-model="form.sexLabel"
                is-link
                label="性别："
                placeholder=""
                readonly
              />

                <van-field
                v-model="form.nativePlace"
                name=""
                label="籍贯："
                readonly
                placeholder=""
                />

                <van-field
                v-model="form.phone"
                name=""
                label="联系电话："
                readonly
                placeholder=""
                />
                
                <van-field
                v-model="form.residentialAddress"
                name="residentialAddress"
                label="家庭住址："
                readonly
                placeholder=""
                />
                <van-field
                v-model="form.eduLevelLabel"
                name="eduLevelLabel"
                label="人才等级："
                readonly
                placeholder=""
                />
                <van-field
                v-model="form.occupation"
                name="occupation"
                label="现职务："
                readonly
                placeholder=""
                />

                <van-field
                v-model="form.postTitleLabel"
                is-link
                readonly
                label="职称："
                placeholder=""
              />

              <van-field
                v-model="form.eduBackgroundLabel"
                is-link
                readonly
                label="学历："
                placeholder=""
              />

              <van-field
                v-model="form.eduDegreeLabel"
                is-link
                readonly
                label="学位："
                placeholder=""
              />

              <van-field
                v-model="form.politicalLabel"
                is-link
                readonly
                label="政治面貌："
              />
              <van-cell title="合同时间：" :value="form.contractStart" />
              
              <van-cell title="到期时间：" :value="form.contractEnd" />
              
              <van-field
                v-model="form.rdLabel"
                is-link
                readonly
                label="是否人大代表："
              />

              <van-field
                v-model="form.zxLabel"
                is-link
                readonly
                label="是否政协委员："
              />
              <van-field
              is-link
              readonly
              label="人才来源地"
              v-model="fieldValue"
            />
              <van-field
              is-link
              readonly
              label="人才头衔: "
              v-model="form.headTitleLabel"
            />
            </van-cell-group>
        </div>
        <div class="qiye-info2" v-if="workhistoryList.length">
          <div class="title">工作经历</div>
          <van-cell-group inset v-for="(item, index) of workhistoryList" :key="index">
            <div class="gzjl">工作经历{{index+1}}: </div>
            <van-cell title="开始时间：" :value="item.startDate" />
            <van-cell title="结束时间：" :value="item.endDate" />
              <van-field
              v-model="item.companyName"
              name="companyName"
              label="单位："
              placeholder=""
              readonly
              />
              <van-field
              v-model="item.job"
              name="job"
              label="职务："
              readonly
              placeholder=""
              />
              <div class="title3">工作经历{{index+1}}信息备注：</div>
              <van-field
              readonly
              style="margin-bottom: 2rem;"
              class="remark"
              type="textarea"
              :rows="3"
              v-model="item.remark"
              name="remark"
              label=""
              placeholder=""
              />
          </van-cell-group>
        </div>
      </van-form>

      <div class="approval-btn" v-if="form.auditStatusLabel == '未审核'">
        <div class="btn1" @click="handleApproved">通过</div>
        <div class="btn2" @click="handleRefuse">退回</div>
      </div>
      <van-dialog class="reasondia" v-model:show="showAuditReason" title="退回备注" show-cancel-button @confirm="dialogConfirm">
        <van-field
        style="margin-bottom: 2rem;"
        class="remark"
        type="textarea"
        :rows="6"
        v-model="auditReason"
        label=""
        placeholder=""
        />
      </van-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { reactive } from 'vue';
import { useRouter,useRoute } from 'vue-router';
import { onMounted } from 'vue';
import { showToast } from 'vant';
import { getDictSelect, approveTalent, getOrigin, getTalentAuditInfo, getWorkHistoryList, editTalentPerson, editWorkHistory,creatTalentPerson,creatWorkHistory } from '@/api/company';
import { showSuccessToast, showFailToast } from 'vant';
const router = useRouter()
const route = useRoute();

const fieldValue = ref('')
// 递归函数,查找省市对应的text
function findTextByValue(originList, value) {
for (let i = 0; i < originList.length; i++) {
    if (originList[i].enCode === value) {
      return originList[i].fullName;
    }
    if (originList[i].children) {
      const fullName = findTextByValue(originList[i].children, value);
      if (fullName) {
        return fullName;
      }
    }
  }
  return null;
}

//省市
var originList = [];
const getOriginList = () => {
getOrigin().then(res => {
  const {code,data} = res
    if(code === 200){
      originList = data
      console.log('originList:',originList);
      console.log('provinceId.value22:',provinceId.value);
      console.log('cityId.value222:',cityId.value);
      const value1 = provinceId.value;
      const value2 = cityId.value;
      const text1 = findTextByValue(originList, value1);
      const text2 = findTextByValue(originList, value2);
      console.log(text1); 
      console.log(text2);
      fieldValue.value = text1 + '/' + text2
    }
  })
}

const provinceId = ref('')
const cityId = ref('')
const personId = ref('')
async function getDetail() {
const id = route.query.auditId || '';
  if(id){
    const { code, data } = await getTalentAuditInfo(id);
    if(code === 200) {
      form.talentCode = data.talentCode
      form.name = data.name
      form.sexLabel = data.sexLabel
      form.companyId = data.companyId
      form.nativePlace = data.nativePlace
      form.phone = data.phone
      form.residentialAddress = data.residentialAddress
      form.eduLevelLabel = data.eduLevelLabel
      form.occupation = data.occupation
      form.postTitleLabel = data.postTitleLabel
      form.eduBackgroundLabel = data.eduBackgroundLabel
      form.eduDegreeLabel = data.eduDegreeLabel
      form.politicalLabel = data.politicalLabel
      form.auditStatusLabel = data.auditStatusLabel
      form.rdLabel = data.rdLabel
      form.zxLabel = data.zxLabel
      form.headTitleLabel = data.headTitleLabel
      form.contractStart = data.contractStart
      form.contractEnd = data.contractEnd
      form.auditReason = data.auditReason
      provinceId.value = data.province
      cityId.value = data.city
      personId.value = data.personId
    }
  }
}
const workhistoryList = ref([])
const getWorkHistory = async () => {
// const personId = personId.value || '';
  if(personId.value){
    const { code, data } = await getWorkHistoryList({
      personId: personId.value || ''
    });
    if (code === 200 && data.list.length) {
      workhistoryList.value = data.list
    }
  }
}

const form = reactive({
talentCode: '',
name: '',
nativePlace: '',
residentialAddress: '',
occupation: '',
contractStart: '',
contractEnd: '',
phone: '',
eduLevelLabel: '',
postTitleLabel: '',
eduBackgroundLabel: '',
sexLabel: '',
politicalLabel: '',
rdLabel: '',
zxLabel: '',
province: '',
city: '',
companyId: localStorage.getItem('companyId'),
})

const params1 = {
  auditId: route.query.auditId,
  auditStatus: '1',
}
const handleApproved = async ()=> {
  const { code,msg } = await approveTalent(params1);
    if (code === 200) {
      showToast(msg)
      router.push({ path: '/Approval', query: { status: '0' }})
    }
}

const showAuditReason = ref(false);
const auditReason = ref('')
const handleRefuse = async ()=> {
  showAuditReason.value = true
}
const dialogConfirm = async()=>{
  console.log('showAuditReason:',auditReason.value);
  const { code, msg } = await approveTalent({
    auditId: route.query.auditId,
    auditStatus: '2',
    auditReason: auditReason.value
  });
    if (code === 200) {
      showToast(msg)
      router.push({ path: '/Approval', query: { status: '0' }})
    }
}


onMounted(() => {
  getDetail()
  setTimeout(()=>{
    getWorkHistory()
    getOriginList()
  },1000)
})
</script>


<style lang="scss" scoped>
.container {
background: #f9f9f9;
overflow: hidden;
margin-bottom: 6.5rem;
padding: 0 1.3rem;
}
.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  background-color: #fff;
}
.addbtn{
position: fixed;
bottom: 2rem;
width: 92%;
left: 4%;
}
.van-form{
padding: 1.4rem 0 0 0;
}
.van-cell-group{
background: #fff;
}
.van-field{
border-bottom: 1px solid #E0E0E4;
padding: 2rem 0 1.6rem;
}
.title{
  font-size: 1.8rem;
  font-weight: 700;
  margin: 0 0 0.5rem 1.2rem;
}
.qiye-info,.qiye-info2{
  background: #fff;
  padding-top: 1.2rem;
}
.qiye-info2{
margin: 2rem 0 0;
}
.qita{
  margin-top: 2rem;
  padding: 1.2rem;
  background: #fff;
  margin-bottom: 0.6rem;
}
.title2{
  font-size: 1.8rem;
  font-weight: 700;
  margin: 0 0 0.5rem 0;
}
.title3{
  font-size: 1.4rem;
  font-weight: 400;
  margin: 2.5rem 0 1.2rem 0;
}
.remark{
  border: 1px solid #E0E0E4;
  padding: 0.4rem 1rem!important;
}

.van-button{
font-size: 1.8rem;
font-weight: 400;
}
.van-cell{
border-bottom: 1px solid #E0E0E4;
padding: 2rem 0 1.6rem;
}
::v-deep .van-cell__title{
width: 10rem !important;
flex: none;
margin-right: 1.2rem;
}
::v-deep .van-cell__value{
text-align: left;
color: #323233;
}
::v-deep .van-cell__right-icon{
display: none;
}
::v-deep .van-cell-group{
margin-bottom: 0.5rem;
}
.gzjl{
font-size: 1.4rem;
font-weight: 700;
margin-top: 1rem;
}

::v-deep .van-field__label{
width: 10rem!important;
}

.approval-btn{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 3rem 0 0;
  position: fixed;
  bottom: 2rem;
  left: 0;
  width: 100%;
  height: 4.5rem;

  .btn1{
    width: 40%;
    height: 4.5rem;
    line-height: 4.5rem;
    text-align: center;
    background: #1052CC;
    font-size: 1.8rem;
    font-weight: 400;
    color: #fff;
    border: 1px solid #1052CC;
    left: 5%;
    position: absolute;
    bottom: 0;
  }
  .btn2{
    width: 40%;
    height: 4.5rem;
    line-height: 4.5rem;
    text-align: center;
    background: #fff;
    font-size: 1.8rem;
    font-weight: 400;
    color: #333333;
    border: 1px solid #1052CC;
    right: 5%;
    position: absolute;
    bottom: 0;
  }
}
::v-deep .van-dialog__header{
  margin: 1rem;
  text-align: left;
  padding-top: 0;
}

::v-deep .van-dialog{
  width: 100%;
  padding: 1rem;
  background: #fff!important;
}

.thbz{
  border-radius: 5px;
  background: #F2F6FF;
  padding: 1.2rem 1.8rem;
  font-size: 1.4rem;
  font-weight: 400;
  color: #333333;
  margin-bottom: 1.6rem;
  line-height: 2rem;
}

</style>